package com.benyanyi.floatinginputbox;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.benyanyi.floatinginputbox.callback.DefaultEditorCallback;

/**
 * @author YanYi
 * @date 2020/12/18 17:53
 * @email ben@yanyi.red
 * @overview
 */
public class FloatingInputBoxDialog {

    private final Context mContext;
    private final boolean isDefault;
    private View mView;
    private DefaultUiConfig defaultUiConfig;
    private String editMsg;
    private DefaultEditorCallback defaultEditorCallback;
    private Dialog mDialog;

    FloatingInputBoxDialog(Context context, View mView) {
        this.mContext = context;
        this.isDefault = false;
        this.mView = mView;
    }

    FloatingInputBoxDialog(Context context, DefaultUiConfig uiConfig, String editMsg, DefaultEditorCallback editorCallback) {
        this.mContext = context;
        this.isDefault = true;
        this.defaultUiConfig = uiConfig;
        this.editMsg = editMsg;
        this.defaultEditorCallback = editorCallback;
    }

    private void show() {
        mDialog = new Dialog(mContext, R.style.dialogstyle);
        log(isDefault);
        if (isDefault) {
            mDialog.setContentView(R.layout.default_input_box);
            TextView cancel = mDialog.findViewById(R.id.tv_cancel);
            cancel.setText(defaultUiConfig.getLeftText());
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                    if (defaultEditorCallback != null) {
                        defaultEditorCallback.onCancel();
                    }
                }
            });
            TextView title = mDialog.findViewById(R.id.tv_title);
            title.setText(defaultUiConfig.getTitle());
            final EditText editText = mDialog.findViewById(R.id.et_write);
            editText.setHint(defaultUiConfig.getHint());
            editText.setText(editMsg);
            TextView submit = mDialog.findViewById(R.id.tv_send);
            submit.setText(defaultUiConfig.getRightText());
            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                    if (defaultEditorCallback != null) {
                        defaultEditorCallback.onSubmit(editText.getText().toString());
                    }
                }
            });
        } else {
            if (mView != null) {
                mDialog.setContentView(mView);
            }
        }
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setCancelable(true);
        Window window = mDialog.getWindow();
//        window.setContentView(R.layout.default_input_box);
        WindowManager.LayoutParams pl = window.getAttributes();
        pl.gravity = Gravity.BOTTOM;
        pl.width = WindowManager.LayoutParams.MATCH_PARENT;
        pl.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setAttributes(pl);
        mDialog.show();
        mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (defaultEditorCallback != null) {
                    defaultEditorCallback.onCancel();
                }
            }
        });
    }

    private void log(Object msg) {
        Log.v("弹窗", msg.toString());
    }

    /**
     * Dialog中隐藏软键盘
     *
     * @param activity
     */
    void HideSoftKeyBoardDialog(Activity activity) {
        try {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
        } catch (Exception ex) {

        }
    }

    public static void openInputBox(Context context, View mView) {
        FloatingInputBoxDialog inputBoxDialog = new FloatingInputBoxDialog(context, mView);
        inputBoxDialog.show();
    }

    public static void openDefaultInputBox(Context context, DefaultEditorCallback defaultEditorCallback) {
        openDefaultInputBox(context, new DefaultUiConfig(), "", defaultEditorCallback);
    }

    public static void openDefaultInputBox(Context context, String editMsg, DefaultEditorCallback defaultEditorCallback) {
        openDefaultInputBox(context, new DefaultUiConfig(), editMsg, defaultEditorCallback);
    }

    public static void openDefaultInputBox(Context context, DefaultUiConfig defaultUiConfig, DefaultEditorCallback defaultEditorCallback) {
        openDefaultInputBox(context, defaultUiConfig, "", defaultEditorCallback);
    }

    public static void openDefaultInputBox(Context context, DefaultUiConfig defaultUiConfig, String editMsg, DefaultEditorCallback defaultEditorCallback) {
        DefaultUiConfig uiConfig = defaultUiConfig;
        if (uiConfig == null) {
            uiConfig = new DefaultUiConfig();
        }
        FloatingInputBoxDialog inputBoxDialog = new FloatingInputBoxDialog(context, uiConfig, editMsg, defaultEditorCallback);
        inputBoxDialog.show();
    }

}
